package com.manor.im.fragment

import android.os.Bundle
import android.widget.ImageView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.manor.common.fragment.BaseFragment
import com.manor.common.utils.ToastUtil
import com.manor.im.R
import com.tencent.qcloud.tim.uikit.bean.MsgFriendBean
import com.manor.im.data.FRIEND_TYPE_FANS
import com.manor.im.data.FRIEND_TYPE_SHARE
import com.manor.im.data.FriendLiveData
import com.manor.im.dialog.FriendSearchPopup
import com.manor.im.event.MineBookEvent
import kotlinx.android.synthetic.main.fragment_invite_fans.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 创建群聊----邀请分享好友
 * @date  2020/7/21  14:29
 * @version 1.0
 */
class InviteShareFragment : BaseFragment() {

    private var mAdapter: MAdapter? = null

    companion object {
        fun newInstance(): InviteShareFragment {
            val args = Bundle()
            val fragment = InviteShareFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutRes(): Int = R.layout.fragment_invite_fans

    override fun initView() {
        //        R.layout.fragment_mine_friend
//        R.layout.item_mine_friend
        mAdapter = MAdapter(R.layout.item_mine_friend, FriendLiveData.shareList.value)
        mRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mRecycler.adapter = mAdapter
        mAdapter?.setOnItemChildClickListener(itemChildClickListener)
        FriendLiveData.changeEditStatus(true, FRIEND_TYPE_FANS)
        FriendLiveData.shareList.observe(this, observer)
        EventBus.getDefault().register(this)

        tvSearch.setOnClickListener {
            if (FriendLiveData.shareList.value != null && activity!=null){
                FriendSearchPopup.load(requireActivity(), FRIEND_TYPE_SHARE)
            }
        }
    }

    override fun onResume() {
        super.onResume()
    }


    override fun onDestroyView() {
        EventBus.getDefault().unregister(this)
        super.onDestroyView()
    }

    private val observer = Observer<MutableList<MsgFriendBean>> {
        if (FriendLiveData.shareList.value?.size ?: 0 != mAdapter?.data?.size ?: 0) {
            ToastUtil.show("size = ${it.size} ")
            FriendLiveData.shareList.value?.let { mAdapter?.setNewInstance(it) }
        }
        mAdapter?.notifyDataSetChanged()
    }



    private val itemChildClickListener = OnItemChildClickListener { _, view, position ->
        when (view.id) {
            R.id.tvButton -> {
                val item = FriendLiveData.getPos(position, FRIEND_TYPE_SHARE)
                item?.run {
                     FriendLiveData.changeCheckStatus(position, FRIEND_TYPE_SHARE)
                }
            }
        }

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onListChange(e: MineBookEvent) {
        if (e.isEdit) {
            FriendLiveData.changeEditStatus(true, FRIEND_TYPE_SHARE)
        } else {
            FriendLiveData.changeEditStatus(false, FRIEND_TYPE_SHARE)
            FriendLiveData.unCheckAll(FRIEND_TYPE_SHARE)
        }
    }


    private class MAdapter(layout: Int, data: MutableList<MsgFriendBean>?)
        : BaseQuickAdapter<MsgFriendBean, BaseViewHolder>(layout, data) {

        init {
            addChildClickViewIds(R.id.tvButton)
        }

        override fun convert(holder: BaseViewHolder, item: MsgFriendBean) {
            item.run {
                Glide.with(holder.getView<ImageView>(R.id.ivImage))
                        .load(avatarThumb)
                        .circleCrop()
                        .into(holder.getView(R.id.ivImage))
                holder.setText(R.id.tvName, userNickname)
                        .setText(R.id.tvCell, "淘米号：$serialNo")

                holder.setVisible(R.id.tvButton, true)
                holder.getView<ImageView>(R.id.tvButton).isSelected = item.isChecked

            }

        }


    }


}
